/*
	用户新增
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

// dom
$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer, form = layui.form;

		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };	

		function addNewUser(data){
			var _ajaxData = $.extend(ajaxData, data);
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

			$.ajax({
				url: BASE_SERVICEURL + URlS.admin_create_user_for_occasion,
				type: 'POST',
				data: _ajaxData,
				success: function(r){
					var code = r.msg.code;
					if(code == 200){
						layer.close(loadIndex)
						console.log(r)
						//计算一次用户vip等级
						countUserAccumulativeConsumption({uId: r.data.uId})
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(r){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000});
				}
			})
		};

		function countUserAccumulativeConsumption(data) {
			/*	
				统计用户金额变动对用户vip等级的影响
				使用方式：
					序列号
					订单完成
					订单退单
				@param ajaxData 用户请求数据 uid
				接口方式 
			*/
			var _ajaxData = data;
			$.ajax({ 
				url: BASE_SERVICEURL + URlS.userVipLevelAPI, 
				type: 'GET', 
				data: _ajaxData,
				success: function(r){
					console.log(r)
					if(r.msg.code == 200){
						layer.msg('添加用户并更新用户VIP等级信息成功！')
						window.location.reload();
					}
				},
				error: function(r){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000});
				}
			});
		}
		//提交新增
		form.on('submit(add)', function(data){
			//console.log(data);
			var f = data.field;
			addNewUser({
				amount: langTransObj(f.cn, f.tw, f.en),
				uBirthday: f.uBirthday,
				uMail: f.uMail,
				uName: f.uName,
				uPhone: f.uPhone,
				uSex: f.uSex,
				uAddr: f.uAddr
			})
			return false;
		})
	})
})


